<template>
	<view>
		<view class="head">
			<image src="../../static/img/detail01.png" mode="" class="head-bg"></image>
			<view class="head-con flex fljc flai">
				<view>
					<image :src="info.avatar" mode="" class="logo" v-if="info.avatar"></image>
					<image src="../../static/img/noLogo.png" mode="" class="logo" v-else></image>
				</view>
				<view class="head-left">
					<view class="company-name line-clamp-one" v-if="info.name">{{info.name}}</view>
					<view class="no-company-name line-clamp-one" v-else>暂无跑腿公司，请选择</view>

					<view class="select-company flex flai fljc" @click="selectCompany">
						<text v-if="info.name">重新选择</text>
						<text v-else>选择跑腿</text>
						<image src="../../static/img/arrow-right.png" mode=""></image>
					</view>
				</view>
			
				
			</view>
		</view>
		<view class="know-box">
			<view class="title">下单流程</view>
			<view class="propt" @click="isShowFlow=true">点击查看下单流程，轻松购物</view>
		</view>
		<view style="height: 30rpx;"></view>
		<view class="flow" v-if="isShowFlow">
			<view class="flex fljcsb flow-box">


				<view class="flow-list" v-for="(item,index) in flowList" :key="index">
					<text>{{index}}</text>
					<text>{{item}}</text>
				</view>


			</view>

			<image src="../../static/img/up_gray.png" mode="" class="hide" @click="isShowFlow=false"></image>
		</view>
		<view class="package-box" v-if="list.length>0">
			<view class="package-list" v-for="(item,index) in list" :key="index">
				<view class="package-head flex flai fljcsb">
					<view class="head-title flex1" @click="showHand(index)">
						<text>包裹{{index+1}}</text>
						<image src="../../static/img/arrow-right.png" mode=""></image>
					</view>
					<view class="edit" @click="editPack(item.packId)">
						<image src="../../static/img/edit.png" mode=""></image>
					</view>
					<view class="del-pack" @click="delPackage(index,item.packId)">
						<image src="../../static/img/delIcon.png" mode="" class="del"></image>
					</view>

				</view>
				<view class="more-store-box" v-if="item.isShow">
					<view class="store-list flex" v-for="(secondItem,secondIndex) in item.products" :key="secondIndex">
						<view class="store-left flex1">
							<view class="name">{{secondItem.goodsName}}</view>
							<view class="store-name flex flai">
								<image src="../../static/img/store001.png" mode=""></image>
								<text>{{secondItem.merchantName}}</text>
							</view>
							<view class="store-address flex flai">
								<image src="../../static/img/ding02.png" mode=""></image>
								<text>{{secondItem.storeLocation}}</text>
							</view>
						</view>
						<view class="store-buy-num flex flai">
							<image src="../../static/img/sumIcon.png" mode="" @click="sum(index,secondIndex,secondItem.id)"></image>
							<input type="text" v-model="secondItem.number" class="flex1" disabled>
							<image src="../../static/img/addIcon.png" mode="" @click="add(index,secondIndex,secondItem.id)"></image>
						</view>

					</view>

				</view>
			</view>
		</view>
		<view class="no-data" v-else>
			<image src="../../static/img/noData.png" mode=""></image>
			<text>暂无包裹，请点击新增包裹</text>
		</view>
		<view style="height: 170rpx;"></view>
		<view class="footer flex flai fljcsb" v-if="list.length>0">
			<view class="foote-btn" @click="addpack">新增包裹</view>
			<view class="foote-btn" @click="checkOrder">下单</view>
		</view>
		<view class="footer-box" v-else>
			<view @click="addpack">新增包裹</view>
		</view>
	</view>
</template>

<script>
	import API from '../../api/index.js'
	export default {
		data() {
			return {
				num: 12,
				//list:[],
				list: [],
				isShowFlow: false,
				info: {
					name:'',
					avatar:''
				},
				flowList: {}
			}
		},
		onShow(opt) {
			if (uni.getStorageSync('companyInfo')) {
				this.info = uni.getStorageSync('companyInfo')
			}
			this.list = []
			this.getList()
			
		},
		methods: {
			getList() {
				API.indexHome.myShopCar({
					page: 1,
					size: 50
				}).then(res => {
					if (res.errno == '0') {
						var steps = res.data.steps[0]

						this.flowList = steps
						var pack = res.data.packs
						for (var k in pack) {
							if (k==0) {
								pack[k]['isShow'] = true
							} else {
								pack[k]['isShow'] = false
							}
							
						}
						var companyInfo = {
							id:res.data.errandId,
							name:res.data.errandName,
							avatar:res.data.errandAvatar,
							desc:''
						}
						uni.setStorageSync('companyInfo',companyInfo)
						this.list = pack
					}
				})
			},
			showHand(index) {
				var list = this.list

				list[index].isShow = !list[index].isShow
			},
			delPackage(index, id) {
				console.log(index)
				var list = this.list
				list.splice(index, 1)
				this.list = list
				API.indexHome.delPackage({
					packId: id
				}).then(res => {

				})
			},
			// 编辑包裹
			editPack(id) {
				uni.setStorageSync('packId',id)
				uni.navigateTo({
					url: '/pages/addAndEditPackage/addAndEditPackage?type=edit'
				})
			},
			// 选择公司
			selectCompany() {
				uni.navigateTo({
					url: '/pages/selectRunCompany/selectRunCompany?id=' + this.info.id
				})
			},
			// 加
			add(index, secondIndex, id) {
				var list = JSON.parse(JSON.stringify(this.list))
				list[index].products[secondIndex].number = list[index].products[secondIndex].number + 1
				this.list = list
				var num = list[index].products[secondIndex].number
				this.carNumPost(id, num)
			},
			// 减
			sum(index, secondIndex, id) {
				var list = JSON.parse(JSON.stringify(this.list))
				if (list[index].products[secondIndex].number == 1) {
					return
				} else {
					list[index].products[secondIndex].number = list[index].products[secondIndex].number - 1
					this.list = list
				}
				var num = list[index].products[secondIndex].number
				this.carNumPost(id, num)
			},
			carNumPost(id, num) {
				API.indexHome.addAndSumCarNum({
					itemid: id,
					number: num
				}).then(res => {
					if (res.ennro == '0') {

					}
				})
			},
			// 新增包裹
			addpack() {
				uni.removeStorageSync('packId')
				uni.navigateTo({
					url: '/pages/addAndEditPackage/addAndEditPackage?type=add'
				})
			},
			showToast(msg) {
				uni.showToast({
					title: msg,
					duration: 2000,
					icon: 'none'
				});
				
			},
			// 下单
			checkOrder() {
				var list = this.list
				console.log(this.info)
				if (this.info.name=='') {
					this.showToast('请选择跑腿公司')
					return false
				}
				for (var k in list) {
					console.log(list[k].fjrName)
					if (list[k].fjrName=='') {
						var msg = '包裹'+ (Number(k)+1) + '地址信息不能为空,请编辑'
						this.showToast(msg)
						return false
					}
					console.log('88')
				}
				uni.navigateTo({
					url: '/pages/orderInfo/orderInfo?data=' + JSON.stringify(this.list)
				})
			}

		}
		
	}
</script>

<style>
	@import url("./index.css");
</style>
